package org.september.pisces.filestore.service.hik;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.httpclient.methods.GetMethod;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class HikPoolService {

    @Value("${hik.oss.ip:}")
    private String ip;
    @Value("${hik.oss.port:}")
    private String port;
    @Value("${hik.oss.accessKey:}")
    private String accessKey;
    @Value("${hik.oss.secretKey:}")
    private String secretKey;

    public static String imagePoolId;
    public static String videoPoolId;

    private static final String IMAGE_URI = "/HikCStor/PoolList?Marker=&MaxKeys=";
    private static final String VIDEO_URI = "/ISAPI/Storage/poolInfoList?marker=&maxKeys=";
    private static final String IMAGE_PREFIX = "hikcstor";
    private static final String VIDEO_PREFIX = "storage";


    public void initPoolSetting() {
        log.info("initPoolSetting:{},{},{},{}", ip, port, accessKey, secretKey);
        String poolString = getPoolList(IMAGE_URI, IMAGE_PREFIX, 0);
        log.info("poolInfo:{}", poolString);
        JSONArray list = new JSONObject(poolString).getJSONArray("List");
        if (list != null && !list.isEmpty()) {
            JSONObject INFO = (JSONObject) list.get(0);
            imagePoolId = INFO.getStr("PoolID");
            log.info("imagePoolId:{}", imagePoolId);
        } else {
            log.info("imagePoolId is null");
        }
        poolString = getPoolList(VIDEO_URI, VIDEO_PREFIX, 1);
        log.info("poolInfo:{}", poolString);
        list = new JSONObject(poolString).getJSONArray("List");
        if (list != null && !list.isEmpty()) {
            JSONObject INFO = (JSONObject) list.get(0);
            videoPoolId = INFO.getStr("poolID");
            log.info("videoPoolId:{}", videoPoolId);
        } else {
            log.info("videoPoolId is null");
        }
    }

    public String getBestNode(String url, String authorizationPrefix, Integer encryption, String contentType) {
        String gmtTime = GmtTimeUtil.getGmtTime();
        String authorization = getAuth(authorizationPrefix, contentType, gmtTime, url, encryption);
        GetMethod getMethod = new GetMethod("http://" + getAddress() + url);
        getMethod.addRequestHeader("Accept-language", "zh-cn");
        getMethod.addRequestHeader("Authorization", authorization);
        getMethod.addRequestHeader("Host", getAddress());
        getMethod.addRequestHeader("Date", gmtTime);
        getMethod.addRequestHeader("Connection", "close");
        getMethod.addRequestHeader("Content-Type", contentType);
        RequestHandler requestHandler = new RequestHandler();
        String result = requestHandler.sendRequest(getMethod);
        return result;
    }

    public String getPoolList(String url, String authPrefix, Integer encryption) {
        String gmtTime = GmtTimeUtil.getGmtTime();
        String authorization = getAuth(authPrefix, "", gmtTime, url, encryption);
        String poolUrl = "http://" + getAddress() + url;
        GetMethod getMethod = new GetMethod(poolUrl);
        getMethod.addRequestHeader("Authorization", authorization);
        getMethod.addRequestHeader("Host", getAddress());
        getMethod.addRequestHeader("Date", gmtTime);
        getMethod.addRequestHeader("Connection", "close");
        RequestHandler requestHandler = new RequestHandler();
        String result = requestHandler.sendRequest(getMethod);

        return result;
    }

    public String getAuth(String prefix, String contentType, String gmtTime, String url, Integer encryp) {
        return AuthorizationUtil.getAuthorization(prefix, accessKey, secretKey, "GET", contentType, gmtTime, url, encryp);
    }

    public String postAuth(String prefix, String contentType, String gmtTime, String url, Integer encryp) {
        return AuthorizationUtil.getAuthorization(prefix, accessKey, secretKey, "POST", contentType, gmtTime, url, encryp);
    }

    public String getAddress() {
        return ip + ":" + port;
    }
}
